package com.gzc.just.play.last.war.common.share.config;

import com.gzc.just.play.last.war.common.share.data.DataAccessService;
import com.gzc.just.play.last.war.common.share.data.impl.SQLiteDataAccessService;
import com.gzc.just.play.last.war.common.share.cache.UnifiedCacheService;
import com.gzc.just.play.last.war.common.share.cache.impl.CaffeineCacheService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.sql.DataSource;

/**
 * All-in-One开发模式自动配置
 * 仅在dev-all-in-one profile下生效
 */
@Configuration
@Profile("dev-all-in-one")
@ConditionalOnProperty(name = "all-in-one.enabled", havingValue = "true", matchIfMissing = true)
public class DevAllInOneAutoConfiguration {

    /**
     * 配置SQLite数据访问服务
     */
    @Bean
    @ConditionalOnMissingBean(DataAccessService.class)
    public DataAccessService sqliteDataAccessService() {
        return new SQLiteDataAccessService();
    }

    /**
     * 配置Caffeine缓存服务
     */
    @Bean
    @ConditionalOnMissingBean(UnifiedCacheService.class)
    public UnifiedCacheService caffeineCacheService() {
        return new CaffeineCacheService();
    }

    /**
     * 配置SQLite数据源
     */
    @Bean
    @ConditionalOnMissingBean(DataSource.class)
    public DataSource sqliteDataSource() {
        return new SQLiteDataSourceConfig().dataSource();
    }

    /**
     * 配置开发环境特定的bean
     */
    @Bean
    public DevAllInOneProperties devAllInOneProperties() {
        return new DevAllInOneProperties();
    }

    /**
     * 开发环境配置属性
     */
    public static class DevAllInOneProperties {
        private boolean debugMode = true;
        private boolean resetOnStartup = false;
        private String databaseFile = "dev_data.db";

        public boolean isDebugMode() { return debugMode; }
        public void setDebugMode(boolean debugMode) { this.debugMode = debugMode; }
        
        public boolean isResetOnStartup() { return resetOnStartup; }
        public void setResetOnStartup(boolean resetOnStartup) { this.resetOnStartup = resetOnStartup; }
        
        public String getDatabaseFile() { return databaseFile; }
        public void setDatabaseFile(String databaseFile) { this.databaseFile = databaseFile; }
    }
}